<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../layui/layui.js"></script>
    <script src="../js/jquery-1.8.0.min.js"></script>
    <script src="../js/ajax.js"></script>
    <link rel="stylesheet" href="../layui/css/layui.css">
    <link rel="stylesheet" href="../css/shousuokuan.css">
</head>

<body>
    <script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-sm  layui-btn-danger" lay-filter="del" lay-event="del">删除</a>
</script>

    <div class="layuimini-container">
        <div class="layuimini-main">
            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">


                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">部门名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="departmentName" id="departmentName" autocomplete="off"
                                            class="layui-input">
                                    </div>
                                    <div class="layui-input-inline">
                                        <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
                                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    </div>
                                </div>

                            </div>


                        </div>

                    </form>

                </div>
            </fieldset>

        </div>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <!-- <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 新增供货商
            </button>
            <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除选中
            </button> -->

            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="add">添加数据</button>

        </div>
    </script>
    <table id="demo" lay-filter="test"></table>


    <script src="../js/host.js"></script>
    <script>
        $.ajaxSetup({
            headers: {
                "token": localStorage.getItem("token")
            }
        });

        layui.use(["table", 'form'], function () {
            var table = layui.table;
            var form = layui.form;
            table.render({
                elem: '#demo'
                , url: host + '/DepartmentInfo' //数据接口
                , headers: {
                    "token": localStorage.getItem("token")
                }
                , page: true //开启分页
                , toolbar: '#toolbarDemo'
                , loading: true
                , cols: [[ //表头
                    { type: 'numbers', title: '序号' }
                    ,{ field: 'id', title: 'ID', sort: true }
                    , { field: 'departmentId', title: '部门编号' }
                    , { field: 'departmentName', title: '部门名称' }
                    , { fixed: 'right', width: 150, align: 'center', toolbar: '#barDemo' }
                ]]
            });
            form.on("submit(add)", function (data) {
                layer.open({
                    type: 2,
                    title: "添加数据",
                    content: "departmentOperator.html",
                    area: ['500px', '300px'],
                    end: function () {
                        table.reload('demo')
                    }
                })
                return false;
            })



            form.on('submit(formDemo)', function (data) {
                //当前容器的全部表单字段，名值对形式：{name: value}】
                table.reload('demo', {
                    where: data.field
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    } //设定异步数据接口的额外参数
                    //,height: 300
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });



            table.on("tool(test)", function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {

                        $.ajax({
                            url: host + "/delDepartment",
                            type: "post",
                            data: { "id": data.id },
                            dataType: "json",
                            async: false,
                            headers: {
                                "token": localStorage.getItem("token")
                            },
                            success: function (data) {
                                layer.msg('删除成功');
                                table.reload('demo')
                            }
                        });

                    });
                } else if (obj.event === 'edit') {
                    console.log(data)
                    layer.open({
                        type: 2,
                        title: '编辑数据',
                        shadeClose: true,
                        maxmin: true, //开启最大化最小化按钮
                        area: ['500px', '300px'],
                        content: './departmentOperator.html',
                        success: function (layero, index) {
                            var body = layer.getChildFrame('body', index)
                            body.find("#id").val(data.id)
                            body.find("#departmentId").val(data.departmentId)
                            body.find("#departmentName").val(data.departmentName)
                        },
                        end: function () {
                            table.reload('demo');
                        }
                    });
                }
            })



        })


    </script>
</body>

</html>